Imports System.Drawing.Drawing2D
Imports BikiSoft.DataStream

Friend Class gaugeRPM
    Implements IStreamRecieve

    Private m_xoffset As Integer
    Private m_yoffset As Integer = -5

    Private mGraphicManager As BufferedGraphicsContext = Nothing
    Private mBackgroundBuffer As BufferedGraphics = Nothing

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DrawPoints(Needle1)
    End Sub

    Public Property Value() As Double
        Get
            Return Needle1.Value
        End Get
        Set(ByVal value As Double)
            If value <> Needle1.Value Then
                Needle1.Value = value
                Me.Refresh()
            End If
        End Set
    End Property
    Public Property subSize() As Double
        Get
            Return Needle1.Length
        End Get
        Set(ByVal value As Double)
            Needle1.Length = value
            Me.Refresh()
        End Set
    End Property
    Public Property offsetX() As Integer
        Get
            Return m_xoffset
        End Get
        Set(ByVal value As Integer)
            m_xoffset = value
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
            Me.Refresh()
        End Set
    End Property
    Public Property offsetY() As Integer
        Get
            Return m_yoffset
        End Get
        Set(ByVal value As Integer)
            m_yoffset = value
            Call Me.gaugeRPM_Resize(Nothing, Nothing)
            Me.Refresh()
        End Set
    End Property

    Private Sub gaugeRPM_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Needle1.Bounds = New Rectangle(m_xoffset, m_yoffset, Me.Width, Me.Height)
        DrawPoints(Needle1)
        Me.Refresh()
    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If Not IsNothing(mBackgroundBuffer) Then mBackgroundBuffer.Render(e.Graphics)
        MyBase.OnPaint(e)
    End Sub
    Private Sub DrawPoints(ByRef sender As ShapeLibrary.Needle) Handles Needle1.onRedrewPoints
        mGraphicManager = BufferedGraphicsManager.Current
        mGraphicManager.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
        mBackgroundBuffer = mGraphicManager.Allocate(Me.CreateGraphics, Me.ClientRectangle)
        mBackgroundBuffer.Graphics.Clear(Me.BackColor)
        If Not IsNothing(Me.BackgroundImage) Then mBackgroundBuffer.Graphics.DrawImage(Me.BackgroundImage, New PointF(0, 0))
        sender.drawGaugePoints(mBackgroundBuffer.Graphics)
    End Sub

    Public Sub RecieveStream(ByVal Stream As DataStream.Stream) Implements DataStream.IStreamRecieve.RecieveStream
        If Stream.contains("RPM") Then
            Me.Value = Stream("RPM").Value
        Else
            Me.Value = 0
        End If
    End Sub

    Public Event RequestRefresh(ByVal sender As Object, ByVal e As System.EventArgs) Implements DataStream.IStreamRecieve.RequestRefresh

    Public Event Restrict(ByVal sender As Object, ByVal e As DataStream.StreamRestrictEventArgs) Implements DataStream.IStreamRecieve.Restrict

    Public Event Seek(ByVal sender As Object, ByVal e As DataStream.StreamSeekEventArgs) Implements DataStream.IStreamRecieve.Seek


    Public Event Close(ByVal sender As Object, ByVal e As System.EventArgs) Implements DataStream.IStreamRecieve.Close

    Public Sub RecieveSeek(ByVal Stream As DataStream.Stream) Implements DataStream.IStreamRecieve.RecieveSeek
        Me.RecieveStream(Stream)
    End Sub
    Public ReadOnly Property RecieverType() As DataStream.StreamRecieveType Implements DataStream.IStreamRecieve.RecieverType
        Get
            Return DataStream.StreamRecieveType.eStreamConsumeOnly
        End Get
    End Property
    Public ReadOnly Property Name1() As String Implements INamed.Name
        Get
            Return Me.Name
        End Get
    End Property
End Class